package com.easemob.chatuidemo.activity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.easemob.chatuidemo.Constant;
import com.easemob.chatuidemo.adapter.SearchAdapter;
import com.easemob.chatuidemo.domain.User;
import com.hcinfos.gsoo.GsooApplication;
import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.config.Urls;
import com.hcinfos.gsoo.enity.IMUser;
import com.hcinfos.gsoo.util.LogUtil;
import com.hcinfos.gsoo.util.LoginUtil;
import com.hcinfos.gsoo.util.UtilHttp;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

public class SearchActivity extends Activity implements OnClickListener {

	private Button search_bt;
	private EditText search_et;
	private ListView listView;
	private ImageView back_iv;
	private ObjectMapper mapper;
	private List<IMUser> list;
	private SearchAdapter adapter;
	private ProgressBar progress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_friend);
		mapper = new ObjectMapper();
		back_iv = (ImageView) findViewById(R.id.back_iv);
		back_iv.setOnClickListener(this);
		search_et = (EditText) findViewById(R.id.search_et);
		listView = (ListView) findViewById(R.id.listView);
		search_bt = (Button) findViewById(R.id.search_bt);
		search_bt.setOnClickListener(this);
		list = new ArrayList<IMUser>();
		adapter = new SearchAdapter(this, list);
		listView.setAdapter(adapter);
		progress = (ProgressBar) findViewById(R.id.progress);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.back_iv:
			finish();
			break;
		case R.id.search_bt:
			String searchStr = search_et.getText().toString();
			if(searchStr.length() == 0){
				Toast.makeText(this, "请输入你要搜索的搜信号", 500).show();
				return;
			}
			progress.setVisibility(View.VISIBLE);
			search(searchStr);
			break;
		}
	}

	public final int DATA_SUCCESS = 0x2210;
	public final int DATA_FAIL = DATA_SUCCESS + 1;

	private void search(String str) {
		HttpUtils http = UtilHttp.getHttpUtils(this);
		RequestParams params = new RequestParams();
		params.addBodyParameter("beanProxy.ggnumber_Like", str);
		http.send(HttpRequest.HttpMethod.POST, Urls.userExt_list, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {

						LogUtil.d("wxf", responseInfo.result);
						try {
							JSONObject obj = new JSONObject(responseInfo.result);
							int state = obj.getInt("state");
							String rows = obj.getString("rows");
							list = mapper.readValue(rows,
									new TypeReference<List<IMUser>>() {
									});
							if (state == 200) {
								hanlder.sendEmptyMessage(DATA_SUCCESS);
							} else {
								hanlder.sendEmptyMessage(DATA_FAIL);

							}
						} catch (Exception e) {
							LogUtil.e("wxf", e.toString());
							hanlder.sendEmptyMessage(DATA_FAIL);

						}

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						LogUtil.e("wxf", error.toString());
						hanlder.sendEmptyMessage(DATA_FAIL);

					}

				});
	}

	Handler hanlder = new Handler() {
		public void handleMessage(android.os.Message msg) {
			progress.setVisibility(View.GONE);
			switch (msg.what) {
			case DATA_SUCCESS:

				if (list != null) {
					refreshContract(list);
					adapter.setDataChange(list);
				}
				break;
			case DATA_FAIL:

				break;
			}
		};
	};

	private void refreshContract(List<IMUser> list) {

		Map<String, User> users = GsooApplication.getInstance()
				.getContactList();
		Iterator<Entry<String, User>> iterator = users.entrySet().iterator();

		while (iterator.hasNext()) {
			Entry<String, User> entry = iterator.next();
			for (int i = 0; i < list.size(); i++) {
				IMUser user = list.get(i);
				if (!entry.getKey().equals(Constant.NEW_FRIENDS_USERNAME)
						&& !entry.getKey().equals(Constant.GROUP_USERNAME))

					if (("x" + user.getGgnumber()).equals(entry.getValue())) {
						user.setFriend(true);
					}
			}
		}
	}
}
